{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0d55a64a",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "\n",
    "student = {\n",
    "    \"姓名\": ['张三', '李四', '王五', '赵六', '钱七'],\n",
    "    \"性别\": ['男', '男', '女', '女', '男'],\n",
    "    \"年龄\": [18, 19, 20, 21, 22],\n",
    "    \"成绩\": [90, 85, 95, 88, 92]\n",
    "}\n",
    "\n",
    "df = pd.DataFrame(student)\n",
    "print(\"打印DataFrame的全部内容：\")\n",
    "print(df)\n",
    "print(\"打印DataFrame的索引：\")\n",
    "print(df.index)\n",
    "print(\"打印DataFrame的列名：\")\n",
    "print(df.columns)\n",
    "print(\"打印DataFrame的值：\")\n",
    "print(df.values)\n",
    "print(\"打印DataFrame各列的数据类型：\")\n",
    "print(df.dtypes)\n",
    "print(\"打印DataFrame的形状（行数和列数）：\")\n",
    "print(df.shape)\n",
    "print(\"打印DataFrame的维度：\")\n",
    "print(df.ndim)\n",
    "print(\"打印DataFrame的元素个数：\")\n",
    "print(df.size)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "61dfaccf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 5 entries, 0 to 4\n",
      "Data columns (total 4 columns):\n",
      " #   Column  Non-Null Count  Dtype \n",
      "---  ------  --------------  ----- \n",
      " 0   姓名      5 non-null      object\n",
      " 1   性别      5 non-null      object\n",
      " 2   年龄      5 non-null      int64 \n",
      " 3   成绩      5 non-null      int64 \n",
      "dtypes: int64(2), object(2)\n",
      "memory usage: 292.0+ bytes\n",
      "None\n"
     ]
    }
   ],
   "source": [
    "print(df.info())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "2b153f1f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   姓名 性别  年龄  成绩\n",
      "0  张三  男  18  90\n",
      "1  李四  男  19  85\n",
      "2  王五  女  20  95\n",
      "3  赵六  女  21  88\n",
      "4  钱七  男  22  92\n"
     ]
    }
   ],
   "source": [
    "print(df.head())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b8bc0e3e",
   "metadata": {},
   "source": [
    "# 需求\n",
    "1. 取前三列，2-4列，后3列\n",
    "2. 取出年龄大于20的行\n",
    "3. 取出来成绩超过90分的行\n",
    "4. 取出来成绩超过平均分的人\n",
    "5. 男女各自的平均分，平均年龄"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "ce269cb7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   姓名 性别  年龄  成绩\n",
      "0  张三  男  18  90\n",
      "1  李四  男  19  85\n",
      "2  王五  女  20  95\n"
     ]
    }
   ],
   "source": [
    "print(df[:3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "0aae160e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   姓名 性别  年龄  成绩\n",
      "2  王五  女  20  95\n",
      "3  赵六  女  21  88\n",
      "4  钱七  男  22  92\n"
     ]
    }
   ],
   "source": [
    "print(df[2:])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "3e33094e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   姓名 性别  年龄  成绩\n",
      "3  赵六  女  21  88\n",
      "4  钱七  男  22  92\n"
     ]
    }
   ],
   "source": [
    "# 假设年龄列名为 '年龄'\n",
    "result = df[df['年龄'] > 20]\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "6fd908d3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   姓名 性别  年龄  成绩\n",
      "2  王五  女  20  95\n",
      "4  钱七  男  22  92\n"
     ]
    }
   ],
   "source": [
    "# 取出来成绩超过90分的行\n",
    "print(df[df['成绩'] > 90])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "5dfb89b6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   姓名 性别  年龄  成绩   平均分\n",
      "2  王五  女  20  95  90.0\n",
      "4  钱七  男  22  92  90.0\n",
      "男性平均分: 89.0\n",
      "女性平均分: 91.5\n"
     ]
    }
   ],
   "source": [
    "# 计算成绩的平均分\n",
    "average_score = df['成绩'].mean()\n",
    "# 筛选出大于平均分的人\n",
    "above_average_people = df[df['成绩'] > average_score]\n",
    "# 复制一份筛选结果，避免在原数据上操作\n",
    "result_df = above_average_people.copy()\n",
    "# 在结果的最后一列添加平均分\n",
    "result_df['平均分'] = average_score\n",
    "print(result_df)\n",
    "\n",
    "# 计算男性和女性的平均分\n",
    "male_average = df[df['性别'] == '男']['成绩'].mean()\n",
    "female_average = df[df['性别'] == '女']['成绩'].mean()\n",
    "print(\"男性平均分:\", male_average)\n",
    "print(\"女性平均分:\", female_average)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "ai",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
